import { http } from '@/utils/http/axios';
import {ContentTypeEnum} from "@/enums/httpEnum";
import {enumsData} from "@/enums/dataEnum";
import {websiteConfig} from "@/config/website.config";

export interface Annex {
  name: string;
  url: string;
}
export interface loginParams {
  clientType: string;
  orgId?: number;
  password: string;
  timeStamp: number | string;
  username: string;
  key: string;
  captcha: string;
}
/**
 * @description: 用户登录
 */
export function login(data) {
  return http.request({
    url: '/login/password',
    method: 'post',
    data: data,
    transformRequest: [
      function (data) {
        return data;
      },
    ],
  });
}
/**
 * @description: 获取用户组织
 */
export function getUserOrg(data) {
  return http.request({
    url: '/login/getUserOrg',
    method: 'post',
    data,
    transformRequest: [
      function (data) {
        return data;
      },
    ],
  });
}
/**
 * @description: 获取登录用户信息
 */
export function getCurrentUserInfo() {
  return http.request({
    url: '/user/common/getUserInfo',
    method: 'get',
  });
}
export function getAnnexList(params) {
  return http.request({
    url: '/upload/annex',
    method: 'get',
    params,
  });
}
export function getCaptchaImage() {
  return http.request({
    url: '/captcha/common/generate',
    method: 'get',
  });
}
export function getApIp() {
  return fetch(`http://${websiteConfig.apServerUrl}/new_ap/get_all_new_ap`).then((res) => res.json()) as Promise<string>
}

export async function uploadFile(fileName, type, data) {
  const formData = new FormData()
  if (type === 'HANDWRITING') {
    const blobData = new Blob([JSON.stringify(data)], { type: 'text/plain' })
    const file = new File([blobData], fileName, { type: 'text/plain' })
    formData.append('fileCategory', type)
    formData.append('multipartFile', file)
  } else if (type === 'COMMON') {
    const buf = await fetch(data).then((r) => r.arrayBuffer())
    const file = new File([buf], fileName, { type: 'image/png' })
    formData.append('fileCategory', type)
    formData.append('multipartFile', file)
  }

  return http.request({
    url: '/file/common/upload',
    method: 'POST',
    data: formData,
    headers: {
      'Content-type': ContentTypeEnum.FORM_DATA,
      ignoreCancelToken: true,
    },
  });
}
export function ocrImage(params) {
  return http.request({
    url: '/student/store/screen/ocrRecognition',
    method: 'get',
    params
  });
}
